package com.itheima.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.tanhua.api.ScoreApi;
import com.itheima.tanhua.domain.Score;
import com.itheima.tanhua.dubbo.mappers.ScoreMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Lff
 * @date 2022/4/8
 */
@DubboService
public class ScoreApiImpl implements ScoreApi {
    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    public Score getMaxCheckPoint(Long uid) {
        Score score = scoreMapper.getMaxCheckPoint(uid);
        return score;
    }

    /**
     * 查询用户最高通关数
     *
     * @param id
     * @param checkpoints
     * @return
     */
    @Override
    public Score findByUserIdAndCheckPoint(Long id, Integer checkpoints) {
        LambdaQueryWrapper<Score> qw = Wrappers.lambdaQuery(Score.class);
        qw.eq(Score::getUid, id)
                .eq(Score::getCheckpoints, checkpoints);
        return scoreMapper.selectOne(qw);
    }


    //保存Score记录
    public boolean save(Score score) {
        int insert = scoreMapper.insert(score);
        return insert > 0;
    }

    /**
     * 根据条件查询指定的Score
     *
     * @param condition
     * @return
     */
    @Override
    public List<Score> findOneByCondition(Score condition) {
        LambdaQueryWrapper<Score> qw = Wrappers.lambdaQuery(Score.class);
        qw.eq(condition.getLevel() != null, Score::getLevel, condition.getLevel())
                .eq(condition.getStars() != null, Score::getStars, condition.getStars())
                .eq(condition.getPersonalityId() != null, Score::getPersonalityId, condition.getPersonalityId());
        return scoreMapper.selectList(qw);
    }

    /**
     * 根据关卡和用户id来获取记录
     *
     * @param checkpoint
     * @param id
     * @return
     */
    @Override
    public Score findByCheckPointAndUserId(int checkpoint, Long id) {
        LambdaQueryWrapper<Score> qw = Wrappers.lambdaQuery(Score.class);
        qw.eq(Score::getCheckpoints, checkpoint).eq(Score::getUid, id);
        return scoreMapper.selectOne(qw);
    }

    @Override
    public void update(Score score) {
        LambdaQueryWrapper<Score> qw = Wrappers.lambdaQuery(Score.class);
        qw.eq(Score::getCheckpoints, score.getCheckpoints()).eq(Score::getUid, score.getUid());
        scoreMapper.update(score, qw);
    }


}
